package com.heima.home.controller;

import com.heima.home.entity.Favorites;
import com.heima.home.service.FavoritesService;
import com.heima.home.util.UserContext;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/favorites")
public class FavoritesController  {
    @Autowired
    private FavoritesService favoritesService;
    
    @GetMapping("/findAll")
    public List<Favorites> findAll(){
        Integer currentUserId = UserContext.getCurrentUserId();
        System.out.println(currentUserId);
        return favoritesService.findAll(currentUserId);
    }

    @DeleteMapping("/cancelFavorites")
    public int cancelFavorites(@RequestParam Integer propertyId){
        Integer userId = UserContext.getCurrentUserId();
        return favoritesService.cancelFavorites(propertyId, userId);
    }
    @PostMapping("/addFavorites")
    public int addFavorites(@RequestBody Favorites favorites){
        Integer userId = UserContext.getCurrentUserId();
        favorites.setUserId(userId);
        return favoritesService.addFavorites(favorites);
    }
}